package com.example.springboot_project.controller;

import com.example.springboot_project.entity.RespInfo;
import com.example.springboot_project.entity.Students;
import com.example.springboot_project.service.StudentsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 获取一个类的字节码对象的三种方式：
* 1.当前类的对象.getClass();
* 2.类名.class
* 3.class.forName("类的全限定类名");  类的全限定类名:包名.类名
* */
//alt+enter 排错键
//@Controller
@RestController //将返回对象转换为JSON字符串

@RequestMapping("/students")
public class StudentsController {

    @Autowired
    private StudentsService studentsService;

    //查询所有学生信息
    @RequestMapping("findAll")
    public RespInfo findAll(){
       List<Students> list = studentsService.findAll();
        System.out.println(list);

        //打印日志
        //Logger logger = LoggerFactory.getLogger(this.getClass().getName());
        Logger logger = LoggerFactory.getLogger(this.getClass());
        //logger.info("students",list);
        logger.info(list.toString());

        //设置返回信息
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(1);
        respInfo.setMsg("获取成功");
        respInfo.setData(list);
        return respInfo;
    }
    //添加
    @RequestMapping("addStudent")
    public RespInfo addStudent(Students students) {
        int a = studentsService.insert(students);
        System.out.println(a);
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(1);
        respInfo.setMsg("添加成功");
        respInfo.setData(a);
        return respInfo;
    }
    //删除
    @RequestMapping("deleteStudentById")
    public RespInfo deleteStudentById(Integer stu_id) {
        int d = studentsService.deleteByPrimaryKey(stu_id);
        System.out.println(d);
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(1);
        respInfo.setMsg("删除成功");
        respInfo.setData(d);
        return respInfo;
    }
    //修改
    @RequestMapping(value = "updateStudent",method = RequestMethod.POST)
    public RespInfo updateStudent(@RequestBody  Students students) {
        int u = studentsService.updateByPrimaryKey(students);
        System.out.println(u);
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(1);
        respInfo.setMsg("修改成功");
        respInfo.setData(u);
        return respInfo;
    }
    //登录
    @RequestMapping("login")
    public RespInfo login(Students stu){
        String phone = "13000000001";
        String pwd = "ligang";
        String stuName = "李刚";
        RespInfo respInfo = new RespInfo();
        Students students = studentsService.selectByStuPhoneAndStuPwdAndStuName(stu.getStuPhone(), stu.getStuPwd(),stu.getStuName());
        if (students!=null){
            respInfo.setCode(1);
            respInfo.setMsg("登录成功");
        }else {
            respInfo.setCode(0);
            respInfo.setMsg("登录失败");
        }
        return respInfo;
    }
    //注册
    @RequestMapping(value = "regist",method = RequestMethod.POST)
    public RespInfo regist(Students students){
        int i = studentsService.insertSelective(students);
        RespInfo respInfo = new RespInfo();
        if (i>0){
            respInfo.setCode(1);
            respInfo.setMsg("注册成功");
        }else {
            respInfo.setCode(0);
            respInfo.setMsg("注册失败");
        }
        return respInfo;
    }
}
